#define  _CRT_SECURE_NO_WARNINGS
class Solution {
public:
    bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
        unordered_map<int, vector<int>> m;
        vector<int> in(numCourses, 0);

        for (auto e : prerequisites)
        {
            int cur = e[0], prev = e[1];
            m[prev].push_back(cur);
            in[cur]++;
        }

        queue<int> q;
        for (int i = 0; i < in.size(); i++)
            if (in[i] == 0) q.push(i);

        while (q.size())
        {
            int cur = q.front(); q.pop();

            for (auto e : m[cur])
            {
                in[e]--;
                if (in[e] == 0)q.push(e);
            }
        }

        for (auto e : in)if (e) return false;
        return true;
    }
};